<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="业务名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入业务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="提成类型" prop="missionType">
        <el-select
          v-model="queryParams.missionType"
          placeholder="请选择提成类型"
          clearable
          size="small">
          <el-option label="按照固定金额" value="0" />
          <el-option label="按照积分" value="1" />
          <el-option label="按照固定百分比" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option label="正常" value="0" />
          <el-option label="已禁用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['commission:basic:add']"
        >
          新增业务</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commissionList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务名称" align="center" prop="businessName" />
      <el-table-column
        label="提成类型"
        align="center"
        prop="missionType"
        :formatter="formatType"
      />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="formatStatus"
      />
      <el-table-column
        label="提成金额/百分比"
        align="center"
        prop="missionAmount"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['commission:basic:del']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status === 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['commission:basic:status']"
            @click="handlerStatus(scope.row)"
            >禁用</el-button
          >
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['commission:basic:status']"
            @click="handlerStatus(scope.row)"
            >启用</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['commission:basic:employee']"
            @click="getMissionPerson(scope.row)"
            >分佣员工</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['commission:basic:edit']"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getMissionRule(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改返佣金额对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="580px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入业务名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
        <el-form-item label="提成设置" prop="missionSetting">
          <el-radio-group v-model="form.missionSetting" :disabled="disabled">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="提成类型"
          prop="missionType"
          v-if="form.missionSetting == 0"
        >
          <el-select v-model="form.missionType" placeholder="请选择提成类型">
            <el-option label="按照固定金额" :value="0" />
            <el-option label="按照积分" :value="1" />
            <el-option label="按照固定百分比" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item
          label="发放条件"
          prop="giveCondition"
          v-if="form.missionSetting == 0"
        >
          <el-radio-group v-model="form.giveCondition">
            <el-radio :label="0">签约成功立即发放</el-radio>
            <el-radio :label="1">签约成功用户每次扣款成功发放</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="金额/百分比"
          prop="missionAmount"
          v-if="form.missionSetting == 0"
        >
          <el-input
            v-model="form.missionAmount"
            placeholder="请输入金额/百分比"
          />
        </el-form-item>
        <el-form-item
          label="添加员工"
          prop="idList"
          v-if="form.missionSetting == 0"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handlePerson(scope.row)"
              >选择员工</el-button
            >
            <!--            {{tableSelectData}}-->
            <!--            {{tableSelectDataNames}}-->
            <template v-for="(item, index) in tableSelectData">
              <el-tag
                :key="item.userId"
                closable
                @close="delSelectData(index, item.userId)"
                >{{ item.userName }}</el-tag
              >
            </template>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="选择员工"
      :visible.sync="open2"
      width="40%"
      :before-close="clearData"
    >
      <el-form>
        <el-form-item prop="keyWord">
          <el-input
            v-model="params.keyWord"
            placeholder="输入用户名称或角色"
            clearable
            size="small"
          />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getPersonList"
          >搜索</el-button
        >
      </el-form>
      <el-table
        :data="gridData"
        v-loading="dialLoading"
        @selection-change="handleSelectionChange2"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          property="userName"
          label="姓名"
          width="120"
        ></el-table-column>
        <el-table-column
          property="roleNames"
          label="角色"
          width="120"
        ></el-table-column>
        <el-table-column
          property="deptName"
          label="所属部门"
          width="120"
        ></el-table-column> </el-table
      ><br /><br />
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        @click="tableSelectClick"
        >确定</el-button
      >
      <pagination
        v-show="logTotal > 0"
        :total="logTotal"
        :page.sync="params.pageNum"
        :limit.sync="params.pageSize"
        @pagination="getPersonList"
      />
    </el-dialog>

    <el-dialog
      title="返佣员工"
      :visible.sync="open3"
      width="40%"
      :before-close="clearData2"
    >
      <el-form>
        <el-form-item prop="keyWord">
          <el-input
            v-model="params.keyWord"
            placeholder="输入用户名称或角色"
            clearable
            size="small"
          />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getMissionPersonList"
          >搜索</el-button
        >
      </el-form>
      <el-table :data="gridData2" v-loading="dialLoading2">
        <el-table-column
          property="userName"
          label="姓名"
          width="120"
        ></el-table-column>
        <el-table-column
          property="roleNames"
          label="角色"
          width="120"
        ></el-table-column>
        <el-table-column
          property="deptName"
          label="所属部门"
          width="120"
        ></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="cancelMission(scope.row)"
              >取消返佣</el-button
            >
          </template>
        </el-table-column> </el-table
      ><br /><br />
      <pagination
        v-show="logTotal2 > 0"
        :total="logTotal2"
        :page.sync="params.pageNum"
        :limit.sync="params.pageSize"
        @pagination="getMissionPersonList"
      />
    </el-dialog>

    <el-dialog
      title="查看信息"
      :visible.sync="open4"
      width="650px"
      append-to-body
      :before-close="clearData3"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="业务名称" prop="businessName">
          <el-input
            v-model="form.businessName"
            placeholder="请输入业务名称"
            readonly
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
            readonly
          />
        </el-form-item>
        <el-form-item label="提成设置" prop="missionSetting">
          <el-radio-group v-model="form.missionSetting" :disabled="disabled">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="提成类型"
          prop="missionType"
          v-if="form.missionSetting == 0"
        >
          <el-select
            v-model="form.missionType"
            placeholder="请选择提成类型"
            :disabled="disabled"
          >
            <el-option label="按照固定金额" :value="0" />
            <el-option label="按照积分" :value="1" />
            <el-option label="按照固定百分比" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item
          label="发放条件"
          prop="giveCondition"
          v-if="form.missionSetting == 0"
        >
          <el-radio-group v-model="form.giveCondition" :disabled="disabled">
            <el-radio :label="0">签约成功立即发放</el-radio>
            <el-radio :label="1">签约成功用户每次扣款成功发放</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="金额/百分比"
          prop="missionAmount"
          v-if="form.missionSetting == 0"
        >
          <el-input
            v-model="form.missionAmount"
            placeholder="请输入金额/百分比"
            readonly
          />
        </el-form-item>
        <el-form-item label="员工信息" v-if="form.missionSetting == 0">
          <el-table :data="gridData3">
            <el-table-column
              property="userName"
              label="姓名"
              width="180"
            ></el-table-column>
            <el-table-column
              property="roleNames"
              label="角色"
              width="180"
            ></el-table-column>
            <el-table-column
              property="deptName"
              label="所属部门"
              width="180"
            ></el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCommission,
  cancelMission,
  delCommission,
  getCommissionInfo,
  listCommission,
  listLog,
  listLog2,
  updateCommission,
  updateStatus,
} from '@/api/commission/commission'

export default {
  name: 'Commission',
  data() {
    return {
      tableSelectData: [],
      tableSelectDataBuffer: [],
      // 遮罩层
      loading: true,
      dialLoading: false,
      dialLoading2: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 返佣金额表格数据
      commissionList: [],
      gridData: [],
      gridData2: [],
      gridData3: [],
      // 弹出层标题
      title: '',
      disabled: false,
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      open4: false,
      logTotal: 0,
      logTotal2: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: null,
        missionType: null,
        status: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      params: {
        pageNum: 1,
        pageSize: 10,
        keyWord: null,
        id: null,
      },
      updateForm: {},
      cancelForm: {},
    }
  },
  computed: {
    tableSelectDataNames() {
      return this.tableSelectData.map((val) => {
        console.log(val)
        return val.userName
      })
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询返佣金额列表 */
    getList() {
      this.loading = true
      listCommission(this.queryParams).then((response) => {
        this.commissionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    delSelectData(index, userId) {
      this.tableSelectData.splice(index, 1)
      for (let i = 0; i < this.ids.length; i++) {
        if (this.ids[i] == userId) {
          this.ids.splice(i, 1)
        }
      }
    },
    // 表单重置
    reset() {
      this.form = {
        businessName: null,
        missionSetting: 1,
        missionType: null,
        status: 0,
        missionAmount: null,
        giveCondition: 0,
        remark: null,
        idList: null,
        businessType: null,
      }
      this.tableSelectData = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handlePerson(row) {
      this.open2 = true
      this.getPersonList()
    },
    getPersonList() {
      this.dialLoading = true
      listLog(this.params).then((response) => {
        this.gridData = response.rows
        this.logTotal = response.total
        this.dialLoading = false
      })
    },
    getMissionPersonList() {
      this.dialLoading2 = true
      listLog2(this.params).then((response) => {
        this.gridData2 = response.rows
        this.logTotal2 = response.total
        this.dialLoading2 = false
      })
    },
    cancelMission(row) {
      this.cancelForm.commissionId = this.params.id
      this.cancelForm.userId = row.userId
      this.$modal
        .confirm('是否确认取消返佣')
        .then(() => {
          return cancelMission(this.cancelForm)
        })
        .then(() => {
          this.getMissionPersonList()
          this.$modal.msgSuccess('取消返佣成功')
        })
        .catch(() => {})
    },
    clearData() {
      this.gridData = []
      this.open2 = false
      this.params.keyWord = null
    },
    clearData2() {
      this.gridData2 = []
      this.open3 = false
      this.params.keyWord = null
    },
    clearData3() {
      this.gridData3 = []
      this.open4 = false
      this.form = []
      this.disabled = false
    },
    formatType(row) {
      if (row.missionType === 0) {
        return '按照固定金额'
      } else if (row.missionType === 1) {
        return '按照积分'
      } else if (row.missionType === 2) {
        return '按照固定百分比'
      }
    },
    formatStatus(row) {
      if (row.status === 0) {
        return '正常'
      } else if (row.status === 1) {
        return '已禁用'
      }
    },

    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length !== 1
    //   this.multiple = !selection.length
    // },
    handleSelectionChange2(selection) {
      this.tableSelectDataBuffer = selection
      // this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    tableSelectClick() {
      this.tableSelectData =
        [...this.tableSelectData, ...this.tableSelectDataBuffer] || []
      this.ids = this.tableSelectData.map((item) => item.userId)
      this.form.idList = this.ids
      this.open2 = false
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加返佣金额'
      this.disabled = false
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getCommissionInfo(id).then((response) => {
        this.form = response.data
        this.tableSelectData = response.data.userList
        this.form.idList = response.data.userList.map((val) => {
          return val.userId
        })
        this.open = true
        this.title = '修改返佣金额'
      })
    },
    handlerStatus(row) {
      this.updateForm.id = row.id
      console.log(row)
      this.updateForm.delFlag = null
      if (row.status === 0) {
        this.updateForm.status = 1
        this.$confirm('是否确认禁用此项')
          .then(() => {
            return updateStatus(this.updateForm)
          })
          .then(() => {
            this.getList()
            this.$modal.msgSuccess('禁用成功')
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        this.updateForm.status = 0
        this.$modal
          .confirm('是否确认开启此项')
          .then(() => {
            return updateStatus(this.updateForm)
          })
          .then(() => {
            this.getList()
            this.$modal.msgSuccess('开启成功')
          })
          .catch(() => {})
      }
    },
    getMissionPerson(row) {
      this.open3 = true
      this.params.id = row.id
      this.getMissionPersonList()
    },

    getMissionRule(row) {
      this.reset()
      const id = row.id
      getCommissionInfo(id).then((response) => {
        this.form = response.data
        this.gridData3 = response.data.userList
        this.open4 = true
        this.disabled = true
      })
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.missionSetting == 1) {
        this.form.missionType = null
        this.form.missionAmount = null
        this.form.giveCondition = null
        this.form.idList = null
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCommission(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCommission(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.updateForm.id = row.id
      this.updateForm.delFlag = 1
      this.$modal
        .confirm('是否确认删除此数据项')
        .then(() => {
          return delCommission(this.updateForm)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
  },
}
</script>
